﻿
games.game6 = {
    firstChosenCard: undefined,
    secondChosenCard: undefined,
    matches: 0,
    init: function () {
        games.game6.firstChosenCard = undefined;
        games.game6.secondChosenCard = undefined;
        games.game6.matches = 0;
        var grid = "0,1,2,3,4,5,6,7,0,1,2,3,4,5,6,7,0,1,2,3,4,5,6,7,0,1,2,3,4,5,6,7,0,1,2,3,4,5,6,7,0,1,2,3,4,5,6,7,0,1,2,3,4,5,6,7,0,1,2,3,4,5,6,7".split(",");
        this.randomiseArray(grid);
        var html = "<ul id='game6-pairs'>";
        for (var i = 0; i < grid.length; i++) {
            html += "<li><a href='#' class='card" + grid[i] + "' id='game6-pairs-card" + i + "'>&nbsp;</li>";
        }
        html += "</ul>";
        $("#gamezone").html(html);
        $("#gamezone ul#game6-pairs li a").click(function () {
            $(this).css("background-image", "url(http://12-games-of-christmas.googlecode.com/svn/trunk/assets/" + $(this).attr("class") + ".jpg)");
            if (games.game6.firstChosenCard == undefined) {
                games.game6.firstChosenCard = $(this);
            } else if (games.game6.secondChosenCard == undefined) {
                games.game6.secondChosenCard = $(this);
                if (games.game6.firstChosenCard.attr("class") == games.game6.secondChosenCard.attr("class") && games.game6.firstChosenCard.attr("id") != games.game6.secondChosenCard.attr("id")) {
                    games.game6.firstChosenCard.unbind('click');
                    games.game6.secondChosenCard.unbind('click');
                    games.game6.firstChosenCard = undefined;
                    games.game6.secondChosenCard = undefined;
                    games.game6.matches++;
                    if (games.game6.matches == 32) {
                        alert("YAY!!!! you did it! Come back tomorow for another game! Or click the date again at the top to reset");
                    }
                } else {
                    setTimeout(function () {
                        var resetBackground = "url(http://12-games-of-christmas.googlecode.com/svn/trunk/assets/back-of-card.png)";
                        games.game6.firstChosenCard.css("background-image", resetBackground);
                        games.game6.secondChosenCard.css("background-image", resetBackground);
                        games.game6.firstChosenCard = undefined;
                        games.game6.secondChosenCard = undefined;
                    }, 500);
                }
            }
        });
    },
    randomiseArray: function (myArray) {
        var i = myArray.length;
        if (i == 0) return false;
        while (--i) {
            var j = Math.floor(Math.random() * (i + 1));
            var tempi = myArray[i];
            var tempj = myArray[j];
            myArray[i] = tempj;
            myArray[j] = tempi;
        }
    }
};